package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.GlobalInfo;
import com.moleq.models.TaxSetting;

public class TaxSettingService {
	private DatabaseHelper DBHelper;
	private String errorString;
	private Context mContext;

	public TaxSettingService() {

	}

	public TaxSettingService(Context context) {
//		this.DBHelper = new DatabaseHelper(context);
		mContext = context;
		this.DBHelper = new DatabaseHelper(GlobalInfo.getInstance().ShareContext);
	}
	
	public List<TaxSetting> FindAll(){
		List<TaxSetting> obj = new ArrayList<TaxSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.TaxSetting.SCHEMA_NAME,null);
//		Cursor cursor = DBHelper.rawQuery("select * from " + DatabaseSchema.TaxSetting.SCHEMA_NAME,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			boolean dtx_by_line= false,dtx_vat = false;
			int tx_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_NO));
			double tx_rate  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_RATE)) * 0.01;
			double tx_start_amt  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_START_AMT));
			double tx_plus_round  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_PLUS_ROUND));
			String tx_vat  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_VAT));
			String tx_by_line  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_BY_LINE));
			
			if(tx_by_line.equals("1")) dtx_by_line = true;
			if(tx_vat.equals("1")) dtx_vat = true;
			if(tx_start_amt == 0) tx_start_amt = 0.01;
			if(tx_plus_round == 0) tx_plus_round = 0.005;

			obj.add(new TaxSetting(tx_no,tx_rate,tx_start_amt,tx_plus_round,dtx_by_line,dtx_vat));
		}
		cursor.close();
		return obj;
	}
	
	public TaxSetting findByNo(int num){
		TaxSetting obj = new TaxSetting();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.TaxSetting.SCHEMA_NAME +" where tx_no = " + num,null);
//		Cursor cursor = DBHelper.rawQuery("select * from "+ DatabaseSchema.TaxSetting.SCHEMA_NAME +" where tx_no = " + num,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return null;
		}
		while(cursor.moveToNext()){
			boolean dtx_by_line= false,dtx_vat = false;
			int tx_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_NO));
			double tx_rate  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_RATE)) * 0.01;
			double tx_start_amt  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_START_AMT));
			double tx_plus_round  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_PLUS_ROUND));
			String tx_vat  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_VAT));
			String tx_by_line  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_BY_LINE));
			
			if(tx_by_line.equals("1")) dtx_by_line = true;
			if(tx_vat.equals("1")) dtx_vat = true;
			
			obj = new TaxSetting(tx_no,tx_rate,tx_start_amt,tx_plus_round,dtx_by_line,dtx_vat);
		}
		cursor.close();
		return obj;
	
	}
	
	public int getTaxCount(){
		
		return 6;
	}
	
	public boolean insertTaxSetting(TaxSetting obj){
		boolean check = true;
		String sql;
		int no;
		double tx_rate,start_amt,plus_round;
		int by_line = 0,vat = 0;
		no = obj.getTx_no();
		tx_rate = obj.getTx_rate();
		start_amt = obj.getTx_start_amt();
		plus_round = obj.getTx_plus_round();
		if(obj.isTx_by_line()) by_line = 1;
		if(obj.isTx_vat()) vat = 1;
		
		sql = "Insert into "+ DatabaseSchema.TaxSetting.SCHEMA_NAME +" values";
		sql += "("+ no +","+ tx_rate +","+ start_amt +","+ plus_round +","+ vat +"," +
				by_line +",strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'))";
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean updateTaxSetting(TaxSetting obj){
		boolean check = true;
		String sql;
		int no;
		double tx_rate,start_amt,plus_round;
		int by_line = 0,vat = 0;
		no = obj.getTx_no();
		tx_rate = obj.getTx_rate();
		start_amt = obj.getTx_start_amt();
		plus_round = obj.getTx_plus_round();
		if(obj.isTx_by_line()) by_line = 1;
		if(obj.isTx_vat()) vat = 1;
		
		sql = "UPDATE "+ DatabaseSchema.TaxSetting.SCHEMA_NAME +" set ";
		sql += "tx_rate = "+ tx_rate +",tx_start_amt = "+ start_amt +",tx_plus_round = "+ plus_round +"," +
				"tx_vat = "+ vat +",tx_by_line = "+ by_line +",update_datetime = strftime('%Y%m%d%H%M%S','now')" +
				" where tx_no = "+ no;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean deleteTaxSetting(int no){
		boolean check = true;
		String sql = "delete from "+ DatabaseSchema.TaxSetting.SCHEMA_NAME +" where tx_no = " + no;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean checkTaxNo(int no){
		boolean result = false;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_tax_setting where tx_no = " + no, null);
//		Cursor cursor = DBHelper.rawQuery("select * from t_tax_setting where tx_no = " + no, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return result;
		}
		if (cursor.moveToNext()) {
			result = true;
		}
		cursor.close();
		return result;
	}
	
	public List<TaxSetting> getTaxByNo(int no){
		List<TaxSetting> obj = new ArrayList<TaxSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_tax_setting where tx_no = " + no, null);
//		Cursor cursor = DBHelper.rawQuery("select * from t_tax_setting where tx_no = " + no, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return null;
		}
		if (cursor.moveToNext()) {
			boolean dtx_by_line= false,dtx_vat = false;
			int tx_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_NO));
			double tx_rate  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_RATE));
			double tx_start_amt  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_START_AMT));
			double tx_plus_round  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_PLUS_ROUND));
			String tx_vat  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_VAT));
			String tx_by_line  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_BY_LINE));
			
			if(tx_by_line.equals("1")) dtx_by_line = true;
			if(tx_vat.equals("1")) dtx_vat = true;
			if(tx_start_amt == 0) tx_start_amt = 0.01;
			if(tx_plus_round == 0) tx_plus_round = 0.005;

			obj.add(new TaxSetting(tx_no,tx_rate,tx_start_amt,tx_plus_round,dtx_by_line,dtx_vat));
		}
		cursor.close();
		return obj;
	}
	
	public List<TaxSetting> getAllTaxSetting(){
		List<TaxSetting> obj = new ArrayList<TaxSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.TaxSetting.SCHEMA_NAME,null);
//		Cursor cursor = DBHelper.rawQuery("select * from " + DatabaseSchema.TaxSetting.SCHEMA_NAME,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return null;
		}
		while(cursor.moveToNext()){
			boolean dtx_by_line= false,dtx_vat = false;
			int tx_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_NO));
			double tx_rate  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_RATE));
			double tx_start_amt  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_START_AMT));
			double tx_plus_round  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_PLUS_ROUND));
			String tx_vat  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_VAT));
			String tx_by_line  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TaxSetting.TX_BY_LINE));
			
			if(tx_by_line.equals("1")) dtx_by_line = true;
			if(tx_vat.equals("1")) dtx_vat = true;
			if(tx_start_amt == 0) tx_start_amt = 0.01;
			if(tx_plus_round == 0) tx_plus_round = 0.005;

			obj.add(new TaxSetting(tx_no,tx_rate,tx_start_amt,tx_plus_round,dtx_by_line,dtx_vat));
		}
		cursor.close();
		return obj;
	}
}
